<?php 
namespace Common\Service; 
use Common\Util\CacheUtils;
use App\Model\VersionModel;
use Common\Util\ConfigUtils;
/**
 * 版本控制工具
 * @author liuyang
 * @version 2.0
 * @since 20160417
 */
class VersionService {
	    
    /**
     获得信息
     */
    public static function getVersion($phoneSystem){
        $deviceType = self::_getTypeByIphoneSystem($phoneSystem);
        //区分设备的类型
        $projectName = ConfigUtils::getConfig("PROJECT_NAME");
        $cacheName = CacheUtils::VERSION_PREFIX.$projectName.$deviceType;
        $obj = CacheUtils::get($cacheName); 
        if($obj==null){
            $versionModel = new VersionModel();
            $obj = $versionModel->getByType($deviceType);
            if($obj)
                CacheUtils::set($cacheName, $obj, 30*24*3600);
        }
        return $obj;
    }
    
    /**
        通过设备获得系统对应的类型
     */
    private static function _getTypeByIphoneSystem($phoneSystem){
        $deviceType =  ConfigUtils::getConfig("ANDROID_TYPE");
        if(strpos(strtolower($phoneSystem), "ios") !== false){
            $deviceType = ConfigUtils::getConfig("IOS_TYPE");;
        }
        return $deviceType;
    }

    /**
     * 获取当前服务器地址
     */
    public static function obtainServerUrl($phoneSystem,$version){
       
        $versionObj = self::getVersion($phoneSystem); 
        //版本号
        $curRealVersion = $versionObj['version'];  
        return ConfigUtils::getConfig('SERVER_URL'); 
    }
    
    /**
     * 获取当前图片服务器地址
     */
    public static function obtainPicServerUrl($phoneSystem,$version){ 
        $versionObj = self::getVersion($phoneSystem);
        //版本号X100
        $curRealVersion = $versionObj['version'];
        if($version>$curRealVersion){
            return ConfigUtils::getConfig('PIC_SERVER_URL');
        }else{
            return ConfigUtils::getConfig('PIC_DEV_SERVER_URL');
        }
    }
    
    /**
     * 根据版本切换数据
     */
    public static function changeDB($phoneSystem,$version){ 
        $versionObj = self::getVersion($phoneSystem);
        //版本号
        $curRealVersion = $versionObj['version'];   
    }
    
 
	 
}
?>